package com.adams.custom.page.mgt.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.adams.access.db.bean.ViewWin;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : ViewWinDaoImpl.java
 * 
 * @Description : 页面窗口实体数据访问实现类
 * @author 904370
 */
public class ViewWinDaoImpl extends HibernateGenericDao<ViewWin, Long>
{

	private static String cacheMapByPk_key = "ViewWin_cacheMapByPk";

	private static String cacheMapByMenuid_key = "ViewWin_cacheMapByMenuid";

	/**
	 * Description : <Detail Description for method>
	 * 
	 * @param model
	 * 
	 */
	public void save(ViewWin model)
	{
		super.save(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.adams.access.db.dao.GenericDao#get(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ViewWin get(Serializable id)
	{
		String key = id + "";
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (!cacheMapByPk.containsKey(key))
		{
			synchronizationCache();
		}
		return (ViewWin) cacheMapByPk.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	public void delete(ViewWin model)
	{
		super.delete(model);
	}

	/**
	 * Description : 删除ViewWin列表
	 * 
	 * @param model
	 * 
	 */
	public void deleteAll(List<ViewWin> models)
	{
		for (ViewWin model : models)
		{
			super.delete(model);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	public void saveOrUpdate(ViewWin model)
	{
		super.saveOrUpdate(model);
	}

	/**
	 * Description : 根据菜单id得到ViewWin列表
	 * 
	 * @param viewid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewWin> findByMenuid(Long menuid)
	{
		String key = menuid + "";
		Map cacheMapByViewid = ViewCacheUtil.getConfigDataCacheMap(cacheMapByMenuid_key);
		if (!cacheMapByViewid.containsKey(key))
		{
			synchronizationCache();
		}
		List<ViewWin> result = new ArrayList<ViewWin>();
		List<ViewWin> temp = (List<ViewWin>) cacheMapByViewid.get(key);
		if (null != temp)
		{
			result.addAll(temp);
			// sort(result);
		}
		return result;
	}

	/**
	 * Description : 根据菜单id及窗口类型得到ViewWin列表
	 * 
	 * @param menuid
	 * @param wintype
	 * @return
	 * 
	 */
	public List<ViewWin> findViewWinByMenuidAndWintype(Long menuid,
			String wintype)
	{
		List<ViewWin> result = findByMenuid(menuid);
		for (Iterator<ViewWin> it = result.iterator(); it.hasNext();)
		{
			ViewWin viewWin = it.next();
			if (!wintype.equals(viewWin.getWintype()))
			{
				it.remove();
			}
		}
		return result;
	}

	private void sort(List<ViewWin> result)
	{
		if (null == result || result.size() == 0)
		{
			return;
		}
		Collections.sort(result, new Comparator<ViewWin>()
		{

			@Override
			public int compare(ViewWin o1, ViewWin o2)
			{
				// TODO Auto-generated method stub
				if (o1.getId() > o2.getId())
				{
					return 1;
				} else if (o1.getId() == o2.getId())
				{
					return 0;

				} else
				{
					return -1;
				}
			}

		});
	}

	/**
	 * Description : 得到所有ViewWin列表
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewWin> findAll()
	{
		List<ViewWin> result = null;
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (cacheMapByPk.size() == 0)
		{
			result = synchronizationCache();
			return result;
		} else
		{
			result = new ArrayList<ViewWin>();
			Set keySet = cacheMapByPk.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();)
			{
				Object key = it.next();
				ViewWin temp = (ViewWin) cacheMapByPk.get(key);
				if (null != temp)
				{
					result.add(temp);
				}
			}
		}
		return result;
	}

	private List<ViewWin> findAllFromDB()
	{
		List<ViewWin> result = query("from ViewWin");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<ViewWin> synchronizationCache()
	{
		List<ViewWin> result = findAllFromDB();
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		Map cacheMapByMenuid = ViewCacheUtil.getConfigDataCacheMap(cacheMapByMenuid_key);
		cacheMapByMenuid.clear();
		for (ViewWin viewWin : result)
		{
			cacheMapByPk.put(viewWin.getId() + "", viewWin);

			String menuidStr = viewWin.getMenuid() + "";
			if (!"".equals(menuidStr))
			{
				List<ViewWin> viewWinList = null;
				if (cacheMapByMenuid.containsKey(menuidStr))
				{
					viewWinList = (List<ViewWin>) cacheMapByMenuid
							.get(menuidStr);
				} else
				{
					viewWinList = new ArrayList<ViewWin>();
				}
				viewWinList.add(viewWin);
				cacheMapByMenuid.put(menuidStr, viewWinList);
			}
		}

		// 排序处理
		Set<String> menuids = cacheMapByMenuid.keySet();
		for (String menuid : menuids)
		{
			List<ViewWin> viewWins = (List<ViewWin>) cacheMapByMenuid
					.get(menuid);
			sort(viewWins);
			cacheMapByMenuid.put(menuid, viewWins);
		}

		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByPk_key, cacheMapByPk);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByMenuid_key, cacheMapByMenuid);
		return result;
	}
}
